import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution6 {
    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> list=new ArrayList<>();
            if(root==null){
                return list;
            }
            Queue<TreeNode> queue=new LinkedList<>();
            // 用长度看列数
            queue.offer(root);
            while(!queue.isEmpty()){
                int size=queue.size();
                List<Integer> list1=new ArrayList<>();
                while(size!=0){
                    TreeNode cur=queue.poll();
                    list1.add(cur.val);
                    size--;
                    if(cur.left!=null){
                        queue.add(cur.left);
                    }
                    if(cur.right!=null){
                        queue.add(cur.right);
                    }
                }
                list.add(list1);
            }
            return list;
        }
    }
}
